/*
#          .,:,,,                                        .::,,,::.          
#        .::::,,;;,                                  .,;;:,,....:i:         
#        :i,.::::,;i:.      ....,,:::::::::,....   .;i:,.  ......;i.        
#        :;..:::;::::i;,,:::;:,,,,,,,,,,..,.,,:::iri:. .,:irsr:,.;i.        
#        ;;..,::::;;;;ri,,,.                    ..,,:;s1s1ssrr;,.;r,        
#        :;. ,::;ii;:,     . ...................     .;iirri;;;,,;i,        
#        ,i. .;ri:.   ... ............................  .,,:;:,,,;i:        
#        :s,.;r:... ....................................... .::;::s;        
#        ,1r::. .............,,,.,,:,,........................,;iir;        
#        ,s;...........     ..::.,;:,,.          ...............,;1s        
#       :i,..,.              .,:,,::,.          .......... .......;1,       
#      ir,....:rrssr;:,       ,,.,::.     .r5S9989398G95hr;. ....,.:s,      
#     ;r,..,s9855513XHAG3i   .,,,,,,,.  ,S931,.,,.;s;s&BHHA8s.,..,..:r:     
#    :r;..rGGh,  :SAG;;G@BS:.,,,,,,,,,.r83:      hHH1sXMBHHHM3..,,,,.ir.    
#   ,si,.1GS,   sBMAAX&MBMB5,,,,,,:,,.:&8       3@HXHBMBHBBH#X,.,,,,,,rr    
#   ;1:,,SH:   .A@&&B#&8H#BS,,,,,,,,,.,5XS,     3@MHABM&59M#As..,,,,:,is,   
#  .rr,,,;9&1   hBHHBB&8AMGr,,,,,,,,,,,:h&&9s;   r9&BMHBHMB9:  . .,,,,;ri.  
#  :1:....:5&XSi;r8BMBHHA9r:,......,,,,:ii19GG88899XHHH&GSr.      ...,:rs.  
#  ;s.     .:sS8G8GG889hi.        ....,,:;:,.:irssrriii:,.        ...,,i1,  
#  ;1,         ..,....,,isssi;,        .,,.                      ....,.i1,  
#  ;h:               i9HHBMBBHAX9:         .                     ...,,,rs,  
#  ,1i..            :A#MBBBBMHB #  .r1,..        ,..;3BMBBBHBB#Bh.     ..                    ....,,,,,i1;   
#   :h;..       .,..;,1XBMMMMBXs,.,, .. :: ,.               ....,,,,,,ss.   
#    ih: ..    .;;;, ;;:s58A3i,..    ,. ,.:,,.             ...,,,,,:,s1,    
#    .s1,....   .,;sh,  ,iSAXs;.    ,.  ,,.i85            ...,,,,,,:i1;     
#     .rh: ...     rXG9XBBM#M#MHAX3hss13&&HHXr         .....,,,,,,,ih;      
#      .s5: .....    i598X&&A&AAAAAA&XG851r:       ........,,,,:,,sh;       
#      . ihr, ...  .         ..                    ........,,,,,;11:.       
#         ,s1i. ...  ..,,,..,,,.,,.,,.,..       ........,,.,,.;s5i.         
#          .:s1r,......................       ..............;shs,           
#          . .:shr:.  ....                 ..............,ishs.             
#              .,issr;,... ...........................,is1s;.               
#                 .,is1si;:,....................,:;ir1sr;,                  
#                    ..:isssssrrii;::::::;;iirsssssr;:..                    
#                         .,::iiirsssssssssrri;;:.
*/
package com.itheima.consumer;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * <dl>
 * <dd>描述: ~ ConsumerListener</dd>
 * <dd>创建时间：  9:07 2020/10/25</dd>
 * <dd>创建人： guodong</dd>
 * <dt>版本历史: </dt>
 * <pre>
 * Date         Author         Version     Description
 * ------------------------------------------------------------------
 * 2020/10/25      guodong         1.0        1.0 Version
 * </pre>
 * </dl>
 */
@Component
//@RabbitListener(queues = "order.A")
@RabbitListener(queues = {"order.A","order_del"})
@Slf4j
public class ConsumerListener {

    @RabbitHandler
    public void receiveMsg(String msg,
                           @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag,
                           @Header(AmqpHeaders.REDELIVERED) boolean reDeliveryTag,
                           Channel channel

    ) {
        /*
                @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, //用于标识消息位置 1,2,3........

                @Header(AmqpHeaders.REDELIVERED) long reDeliveryTag //用于标识该条消息是否重新回到过队列，如果重回过队列值为true

                Channel channel //信道：消息的确认或者消息的删除，目的就是确定该消息究竟是重回队列还是丢失
         */

        log.info("消费者接受到消息： msg: {} 消息位置: {}, 是否重新回到过队列: {}", msg, deliveryTag, reDeliveryTag);
        try {
            Thread.sleep(10000);
            if (msg.contains("苹果")) {
                throw new RuntimeException("不允许购买苹果产品，请购买小米.......");
            }
            /*
                deliveryTag: 消息位置
                multple: 表示是否批量确认的意思，一般是false,不进行批量确认，仅确认自己的消息
             */
            channel.basicAck(deliveryTag, false);
        }catch (Exception e){
            if(reDeliveryTag){ //重回过队列
                //第二次消费也失败了，那么将消息删除
                /*
                    deliveryTag: 消息位置
                    multple: 表示是否批量确认的意思，一般是false,不进行批量确认，仅确认自己的消息
                    requeue: 表示是否要重新回到队列，此处设置为false,不让重新回到队列
                 */
                try {
                    channel.basicNack(deliveryTag, false, false);
                    log.error("第二次消费失败，将消息删除，不让重回队列： e {}", e.getMessage());
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }else {
                //第一次消费失败
                try {
                    //重新回到队列
                    channel.basicNack(deliveryTag, false, true);
                    log.error("第一次消费失败，让消息重新回到队列, e: {}", e.getMessage());
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
}
